#!/usr/bin/ python3
# -*- coding:utf-8 -*-
# ipaparse.py

import os, sys, re, plistlib, zipfile

class ipaparse():
    def __init__(self, ipa_path):
        ipa_file = zipfile.ZipFile(ipa_path)
        
        plist_path = self.find_plist_path(ipa_file)
        plist_data = ipa_file.read(plist_path)
        plist_root = plistlib.loads(plist_data)
        self.get_ipa_info(plist_root)
        self.find_appicon(ipa_file)
        
    def find_plist_path(self,ipa_file):
        name_plist = ipa_file.namelist()
        pattern = re.compile(r'Payload/[^/]*.app/Info.plist')
        for path in name_plist:
            m = pattern.match(path)
            if m is not None:
                return m.group()
    
    def get_ipa_info(self,plist_root):
        self.app_name = plist_root["CFBundleDisplayName"]
        self.app_version = plist_root["CFBundleShortVersionString"]
        self.app_build = plist_root["CFBundleVersion"]
        
    def find_appicon(self,ipa_file):
        file_list = ipa_file.namelist()
        pattern = re.compile(r'Payload/[^/]*.app/[^/]*.png')
        path = ""
        for path in file_list:
            m = pattern.match(path)
            if m is not None:
                path = m.group()
                break
        self.img_file = ipa_file.open(path,"r")
        
    
            
        